package com.guigu.health.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.alibaba.fastjson.JSONObject;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.guigu.health.pojo.Member;
import com.guigu.health.pojo.MemberExample;
import com.guigu.health.pojo.User;
import com.guigu.health.service.MemberService;
import com.mysql.jdbc.StringUtils;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.dao.DataIntegrityViolationException;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import java.text.SimpleDateFormat;
import java.util.*;

/**
 * @ClassName:MemberController
 * @Description:会员控制层
 * @Author:longtao
 * @Date:2021/4/10
 * @Version:1.0
 */
@RequestMapping(value = "/member")
@Controller
public class MemberController extends BaseController{
    @Reference
    private MemberService memberService;

    /**
     * 显示会员数据
     * @param pageNum
     * @param name
     * @return
     */
    @RequiresPermissions(value = {"MEMBER_QUERY"})
    @ResponseBody
    @RequestMapping(value = "/showMember",method = RequestMethod.GET,produces = {"application/json;charset=utf-8"})
    public Object showMember(String pageNum,String name){
        Integer pageNum2=null;
        if (StringUtils.isNullOrEmpty(pageNum)){
            pageNum="1";
        }

        pageNum2=Integer.valueOf(pageNum);
        MemberExample example=new MemberExample();
        PageInfo<Member> pageInfo=null;
        if (!StringUtils.isNullOrEmpty(name)){
            MemberExample.Criteria criteria=example.createCriteria();
            criteria.andNameLike("%"+name+"%");
            pageInfo = memberService.queryAll(example, pageNum2, 2);
        }else {
            pageInfo=memberService.queryAll(null,pageNum2,2);
        }

        String s = JSONObject.toJSONString(pageInfo);
        return s;
    }

    /**
     * 根据id进行删除会员
     * @param id
     * @return
     */
    @RequiresPermissions(value = {"MEMBER_DELETE"})
    @ResponseBody
    @RequestMapping(value = "/deleteMember")
    public Object deleteMember(String id){
        Object b=null;
        try {
            b = memberService.DeleteMemberById(Integer.valueOf(id));
        }catch (DataIntegrityViolationException e){
            b="exception";
        }

        return b;
    }

    /**
     * 用户修改
     * @param member
     * @return
     */
    @RequiresPermissions(value = {"MEMBER_UPDATE"})
    @ResponseBody
    @RequestMapping(value = "/updateMember",method = RequestMethod.POST,produces = {"application/json;charset=utf-8"})
    public Object updateMember(@RequestBody Member member){
        MemberExample example=new MemberExample();
        MemberExample.Criteria criteria=example.createCriteria();
        criteria.andIdEqualTo(Integer.valueOf(member.getId()));
        boolean b = memberService.UpdateMemberById(member, example);
        return b;
    }

    /**
     * 查看过去七天会员注册的总数
     * @param length
     * @return
     */
    @RequiresPermissions(value = {"MEMBER_BROKEN"})
    @ResponseBody
    @RequestMapping(value = "/getMemberCount",method = RequestMethod.GET,produces = {"application/json;charset=utf-8"})
    public Object getMemberCount(String length){
        if (StringUtils.isNullOrEmpty(length)){
            length="7";
        }else if (Integer.valueOf(length)>=15){
            length="15";
        }else if (Integer.valueOf(length)<=7){
            length="7";
        }else{
            length="7";
        }
        List<String> months=new ArrayList<String>();
        Calendar calendar=Calendar.getInstance();
        calendar.add(Calendar.DATE,-Integer.valueOf(length));
        SimpleDateFormat smf=new SimpleDateFormat("yyyy-MM-dd");
        for (Integer i = 0; i < Integer.valueOf(length); i++) {
            calendar.add(Calendar.DATE,+1);
            Date date=calendar.getTime();
            months.add(smf.format(date));
        }
        List<Integer> memberCount=memberService.getCount(months);
        Map<String,Object> map=new HashMap<String,Object>();
        map.put("months",months);
        map.put("orderCount",memberCount);
        return map;
    }
}
